﻿namespace nhappend
{
    using System.Collections.Generic;
    using Data;
    using log4net.Core;

    public static class LoggingEventExt
    {
        public static LogMessage ToEntity(this LoggingEvent logEvent)
        {
            return logEvent == null ? null : new LogMessage
                                                 {
                                                     CreatedDate = logEvent.TimeStamp,
                                                     ErrorMessage = logEvent.GetExceptionString(),
                                                     Level = logEvent.Level.DisplayName,
                                                     Logger = logEvent.LoggerName,
                                                     Message = logEvent.RenderedMessage,
                                                     WorkerThread = logEvent.ThreadName
                                                 };
        }

        public static LogMessage[] ToEntities(this LoggingEvent[] events)
        {
            if (events == null) return null;
            var messages = new List<LogMessage>();

            foreach (LoggingEvent logEvent in events)
            {
                if (logEvent == null) continue;

                messages.Add(logEvent.ToEntity());
            }

            return messages.ToArray();
        }
    }
}
